﻿using System.Linq;
using System.Web.Mvc;
using DMHelper.Logic.DMLogic;

namespace DMHelper.Controllers
{
    public class DmDefaultController : Controller
    {
        readonly DMLogic dmLogicHelper = new DMLogic();
        private const string DmModel = "dmModel";

        public ActionResult Index()
        {
            var model = GetModel();
            model.EncounterStarted = false;

            TempData[DmModel] = model;

            return View(model);
        }

        public ActionResult StartEncounter()
        {
            var model = GetModel();
            dmLogicHelper.RollInitiative(model.Players);
            model.EncounterStarted = true;

            TempData[DmModel] = model;

            return View("Index", model);
        }

        public ActionResult EndPlayerTurn()
        {
            var model = GetModel();
            dmLogicHelper.EndTurn(model.Players, model.InitiativePlayer());

            TempData[DmModel] = model;

            return View("Index", model);
        }

        [HttpGet]
        public ActionResult ChangeRoom(int id)
        {
            var model = GetModel();

            model.CurrentRoom = model.Rooms.First(x => x.Id == id);

            TempData[DmModel] = model;
            return View("Index", model);
        }


        private Models.DmBaseModel GetModel()
        {
            if(TempData[DmModel] == null)
            {
                var model = new Models.DmBaseModel();
                TempData.Add(DmModel, model);
                return model;
            }

            return (Models.DmBaseModel) TempData[DmModel];
        }
    }
}
